import request from '@/utils/request'
import { IResp } from './interfaces'

export interface IUploadResData {
    originalFilename: string,
    newFilename: string,
    url: string
}

type IFolder = 'avatar' | 'article' | 'teachCase'

export const upload = (file: File[], folder: IFolder) => {
    const formData = new FormData()
    file.forEach((item) => {
        formData.append('file', item)
    })
    if (folder) {
        formData.append('folder', folder)
    }
    return request.post<any, IResp<IUploadResData[]>>(
        '/upload',
        formData,
        {
            headers: {
                'Content-Type': 'multipart/form-data',
            },
        }
    )
}

export const uploadKnowledgeBase = (file: File[], data: {
    knowledgeBaseId: number,
    collectionName: string,
    type: string,
}) => {
    const formData = new FormData()
    file.forEach((item) => {
        formData.append('file', item)
    })
    Object.keys(data).forEach((key) => {
        formData.append(key, data[key])
    })
    return request.post<any, IResp>(
        '/knowledge-base/upload',
        formData,
        {
            headers: {
                'Content-Type': 'multipart/form-data',
            },
        }
    )
}
